
<!DOCTYPE html>
<html lang="en-us">
<head>
  
  <meta charset="UTF-8" />
  <meta name="description" content="常用的docker操作" />
  <title>
    Docker 常用命令（一） | DenysG的个人博客
  </title>
  
  <meta name="viewport" content="width=device-width,user-scalable=no,maximum-scale=1,initial-scale=1">
  
  <link rel="canonical" href="https://48474.net/post/docker-readme01/"/>
  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
  
  <link rel="stylesheet" href="/css/sanitize.css">
  <link rel="stylesheet" href="/css/responsive.css">
  <link rel="stylesheet" href="/css/highlight_monokai.css">
  <link rel="stylesheet" href="/css/theme.css">
  <link rel="stylesheet" href="/css/custom.css">
  
  
  <link href="https://48474.net//index.xml" rel="alternate" type="application/rss+xml" title="DenysG的个人博客" />
  <link href="https://48474.net//index.xml" rel="feed" type="application/rss+xml" title="DenysG的个人博客" />

  
  

</head>



<body>
<div class="container">
  
  <header role="banner">
    <div class="row gutters">
      <div id="site-title" class="col span_6">
        <h1><a href="https://48474.net/">DenysG的个人博客</a></h1>
        <h2>我必须承认，幸运喜欢照顾勇敢的人。---- 达尔文</h2>
      </div>
      <div id="social" class="col span_6">
        <ul>
          
          
          <li><a href="https://github.com/cnphpbb/" target="_blank">GitHub</a></li>
          
        </ul>
      </div>
    </div>
  </header>


  
  <main id="single" role="main">
    <div class="article-header">
      <h1>Docker 常用命令（一）</h1>
      <div class="meta">
        Nov 5, 2018 &nbsp;
        
          #<a href="/tags/docker">docker</a>&nbsp;
        
          #<a href="/tags/guide">guide</a>&nbsp;
        
      </div>
    </div>
    <article>
      <p>使用了一段时间 <code>docker</code> 了。现整理出一些常用的 <code>docker cli</code> 指令，方便查找。</p>
<p>官方CLI网址 - <a href="https://docs.docker.com/engine/reference/commandline/cli/">Docker CLI</a></p>
<ul>
<li>对开发，测试和运维人员来说，最希望的就是一次创建或配置，可以在任意地方正常运行。</li>
<li>确保了应用运行环境一致性，从而不会再出现 “这段代码在我机器上没问题啊？！” 这类问题。</li>
<li>由于 <code>Docker</code> 确保了执行环境的一致性，使得应用的迁移更加容易。</li>
</ul>
<blockquote>
<p>Docker 好， Docker化 好！ 好！ 好！</p>
</blockquote>
<h3 id="开发常用的指令">开发常用的指令</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="c1"># docker search debian  </span>
</span></span><span class="line"><span class="cl"><span class="c1"># docker pull debian:&lt;tag&gt;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">// 启动一个容器，并且在运行结束后删除容器
</span></span><span class="line"><span class="cl"><span class="c1"># docker run -it --rm debian</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">// 启动一个容器并命名为mydebian
</span></span><span class="line"><span class="cl"><span class="c1"># docker run --name mydebian -it --rm debian</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">//启动容器并执行多个命令
</span></span><span class="line"><span class="cl"><span class="c1"># docker run -it --rm debian sh -c &#34;cmd1 &amp;&amp; cmd2&#34;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">// 启动容器并挂载本机目录
</span></span><span class="line"><span class="cl"><span class="c1"># docker run -it --rm \</span>
</span></span><span class="line"><span class="cl">  --mount <span class="nv">type</span><span class="o">=</span>bind,source<span class="o">=</span>/&lt;host-path&gt;, <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>  <span class="nv">target</span><span class="o">=</span>/&lt;container-path&gt; <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>  debian
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">//上面的简化
</span></span><span class="line"><span class="cl"><span class="c1"># docker run -it --rm -v &lt;host-path&gt;:&lt;container-path&gt; debian</span>
</span></span></code></pre></div><h3 id="创建新的网络并在启动本地容器的时候-指定ip">创建新的网络,并在启动本地容器的时候 <strong>指定ip</strong></h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="c1"># docker network create --subnet=172.20.0.0/16 mynet</span>
</span></span><span class="line"><span class="cl"><span class="c1"># docker run --net mynet --ip 172.20.0.22 -it alpine:3 sh</span>
</span></span></code></pre></div><h3 id="清理操作">清理操作</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="c1"># docker image prune  // 删除临时image</span>
</span></span><span class="line"><span class="cl"><span class="c1"># docker container prune // 删除已经停止的container</span>
</span></span></code></pre></div><h3 id="导入导出image操作">导入导出image操作</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="c1"># docker save debian &gt; debian.tar</span>
</span></span><span class="line"><span class="cl"><span class="c1"># docker load &lt; debian.tar</span>
</span></span></code></pre></div><h3 id="新建和删除image">新建和删除image</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="c1"># docker build -t cnphpbb/php7.1.23-alpine:latest .</span>
</span></span><span class="line"><span class="cl"><span class="c1"># docker commit -a &#34;cnphpbb&#34; -m &#34;php7.1.23&#34; a404c6c174a2  cnphpbb/php7.1.23-alpine:v1</span>
</span></span><span class="line"><span class="cl"><span class="c1"># docker rmi cnphpbb/php7.1.23-alpine</span>
</span></span></code></pre></div><h3 id="启动mysql">启动mysql</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="c1"># docker run -d --restart=always \</span>
</span></span><span class="line"><span class="cl">  --name mysql <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>  --net mynet <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>  --ip 172.20.0.10 <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>  -p 3306:3306 <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>  -v D:<span class="se">\v</span>ar<span class="se">\m</span>ysql:/var/lib/mysql <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>  -e <span class="nv">MYSQL_ALLOW_EMPTY_PASSWORD</span><span class="o">=</span>yes <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>  mysql:5.6
</span></span></code></pre></div><h3 id="启动redis">启动redis</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="c1"># docker run -d --restart=always \</span>
</span></span><span class="line"><span class="cl">  --name redis <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>  --net mynet --ip 172.20.0.18 <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>  -p 6379:6379 <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>  redis:alpine
</span></span></code></pre></div><h3 id="启动一个swoole服务">启动一个swoole服务</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="c1"># docker run -d --restart=always \</span>
</span></span><span class="line"><span class="cl">  --name Bases <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>  --net mynet <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>  --ip 172.20.0.30 <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>  -p 9901:9701 <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>  -v D:<span class="se">\d</span>evlodGit<span class="se">\B</span>ases:/data/Bases <span class="se">\
</span></span></span><span class="line"><span class="cl"><span class="se"></span>  --workdir<span class="o">=</span>/data/Bases
</span></span><span class="line"><span class="cl">  cnphpbb/php7.1.23-alpine:1.2.3 php start.php
</span></span></code></pre></div><blockquote>
<p>使用docker作为开发环境，特别的方便。<br>
本地系统没有任何污染，拯救系统洁癖专用。<br>
尤其是在windows下面，省去了很多麻烦。</p>
</blockquote>

      
      
      
    </article>
    


  </main>
  
  <nav class="pagination-single">
    
      <span class="previous">&larr; <a href="https://48474.net/post/dockerfile-reference-1/" rel="prev">Dockerfile 参考文档 （一）</a></span>
    
    
      <span class="next"><a href="https://48474.net/post/mysql-sql_mode/" rel="next">Mysql 的 sql_mode 配置</a> &rarr;</span>
    
  </nav>


  
  <footer role="contentinfo">
    <div style="text-align:center;">
      
      © 2020 135Get_Team. All rights reserved.
    </div>
  </footer>


</div>

<script src="/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>



</body>
</html>

